/***************************************************************************
 *   Copyright (C) 2009 by Dead_Body                                       *
 *   jamesholodnak@gmail.com                                               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "defines.h"
#include "pce/pce.h"

u8 pce_io_read(u32 address)
{
	u8 ret;

	switch(address & 0x1C00) {
		case 0x0000:
			return(pce_vdc_read(address));
		case 0x0400:
			return(pce_vce_read(address));
		case 0x0C00:
			switch(address & 3) {
				case 0:
					return(pce->timervalue);
				case 1:
					return(pce->timerstatus);
			}
			break;
		case 0x1000:
			ret = pce->joydata >> pce->joyselect;
			return((ret & 0xF) | pce->countrybit);
		case 0x1400:
			switch(address & 3) {
				case 2:
					return(pce->irqmask);
				case 3:
					return(pce->irqstatus);
			}
			break;
	}
	log_message("unhandled io read at $%06X\n",address);
	return(0);
}

void pce_io_write(u32 address,u8 data)
{
	switch(address & 0x1C00) {
		case 0x0000:
			pce_vdc_write(address,data);
			break;
		case 0x0400:
			pce_vce_write(address,data);
			break;
		case 0x0800:
			break;
		case 0x0C00:
			switch(address & 3) {
				case 0:
					pce->timerpreload = data;
					break;
				case 1:
					pce->timerenable = data & 1;
					break;
			}
			break;
		case 0x1000:
			pce->joyselect = (data & 1) << 3;
			break;
		case 0x1400:
			switch(address & 3) {
				case 2:
					pce->irqmask = data;
					break;
				case 3:
					//reset TIQD
					break;
			}
			break;
		default:
			log_message("unhandled io write at $%06X = $%02X\n",address,data);
			break;
	}
}
